package com.f1ashback.bus.controller;

import com.f1ashback.bus.Qo.UserQo;
import com.f1ashback.bus.Vo.UserVo;
import com.f1ashback.bus.common.AjaxResult;
import com.f1ashback.bus.common.JwtUtils;
import com.f1ashback.bus.common.ThreadUtils;
import com.f1ashback.bus.entity.User;
import com.f1ashback.bus.service.UserService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.java2d.pipe.AAShapePipe;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @PostMapping("/applogin")
    public AjaxResult applogin(@RequestBody User user) {
        return userService.applogin(user);
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PutMapping("/appregister")
    public AjaxResult register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        log.warn("正在退出登录");
        ThreadUtils.removeId();
        return AjaxResult.success("已退出");
    }

    /**
     * 用户列表
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult listUser() {
        return userService.listUser();
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user) {
        return userService.add(user);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    public AjaxResult appupdate(@RequestBody User user) {
        log.warn(user.toString());
        return userService.update(user);
    }

    @PutMapping("/mupdate")
    public AjaxResult update(@RequestBody UserVo user){
        return userService.mupdate(user);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/del/{userId}")
    public AjaxResult delete(@PathVariable Long userId) {
        return userService.delete(userId);
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getOne")
    public User getOne(Long userId) {
        User user = userService.getOneUser(userId);
        return user;
    }

    @GetMapping("/mgetOne/{userId}")
    public User mgetOne(@PathVariable Long userId){
        log.warn("userId:{}",userId);
        return userService.getOneUser(userId);
    }

    /**
     * 用户条件查询
     * @param userQo
     * @return
     */
    @PostMapping("/queryUser")
    public AjaxResult queryUser(@RequestBody UserQo userQo){
        return userService.queryUser(userQo);
    }

    @GetMapping("/getAllUserName")
    public AjaxResult getAllUserName(){
        return userService.getAllUserName();
    }
}
